package com.example.ntier.model.bll;

import java.sql.SQLException;
import java.util.List;

import com.example.ntier.model.dal.AbstractDAL;
import com.example.ntier.model.entity.GenericEntity;

public abstract class AbstractBLL<K, E extends GenericEntity<K>> {

	private final AbstractDAL<K, E> abstractDAL;
	private final Validator<K, E> validator;

	public AbstractBLL(AbstractDAL<K, E> abstractDAL, Validator<K, E> validator) {
		this.abstractDAL = abstractDAL;
		this.validator = validator;
	}

	public boolean insert(E entity) throws SQLException {
		boolean result = false;
		if (validator.validateEntity(entity)) {
			result = abstractDAL.insert(entity);
		}
		return result;
	}

	public boolean update(E entity) throws SQLException {
		boolean result = false;
		if (validator.validateEntity(entity)) {
			result = abstractDAL.update(entity);
		}
		return result;
	}

	public boolean delete(E entity) throws SQLException {
		return abstractDAL.delete(entity);
	}

	public E findById(K id) throws SQLException {
		return abstractDAL.findById(id);
	}

	public List<E> getAll() throws SQLException {
		return abstractDAL.getAll();
	}

	public int getCount() throws SQLException {
		return abstractDAL.getCount();
	}

	public AbstractDAL<K, E> getAbstractDAL() {
		return abstractDAL;
	}
}
